package com.jiwei.system.serviceImpl;

import com.jiwei.system.pojo.Task;
import com.jiwei.system.service.RepairDataService;
import com.jiwei.system.service.RepairService;
import com.jiwei.system.dao.RepairTaskMapper;
import com.jiwei.system.utils.OSSUtil;
import com.sun.org.apache.xerces.internal.util.SynchronizedSymbolTable;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import org.w3c.dom.ls.LSInput;

import javax.annotation.Resource;
import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service("RepairService")
public class RepairServiceImpl implements RepairService,RepairDataService {

    @Resource
    private RepairTaskMapper repairTaskMapper;

    @Resource
    private OSSUtil ossUtil;

    @Transactional(rollbackFor=Exception.class)
    @Override
    public String acceptRepair(Task task) {
        List<MultipartFile> files = task.getFiles();
//        System.out.println(task);
//        System.out.print("files情况为：");
//        System.out.println(files.get(0).getOriginalFilename());
        if(files==null){  //没有图片
            repairTaskMapper.insertRepairTask(task);
            String taskId = task.getId();
            Map<String, Object> attribute = new HashMap<>();
            attribute.put("id",null);
            attribute.put("tasklog",null);
            attribute.put("taskid",taskId);
            repairTaskMapper.insertRepairTaskLog(attribute);
            return "上传成功";
        }else{
            List<String> url = ossUtil.checkList(files);
            if(url.get(0)=="图片太大"){
                return "图片太大";
            }else if(url.get(0)=="上传失败"){
                return "上传失败";
            }else{
                repairTaskMapper.insertRepairTask(task);
                String taskId = task.getId();
                Map<String, Object> attribute = new HashMap<>();
                attribute.put("id",null);
                attribute.put("tasklog",null);
                attribute.put("taskid",taskId);
                repairTaskMapper.insertRepairTaskLog(attribute);
                System.out.print("url情况为：");
                System.out.println(url);
                Map<String, Object> attribute1 = new HashMap<>();
                attribute1.put("id",null);
                attribute1.put("taskid",taskId);
                for(int i = 0;i<url.size() ;i++){
                    String imurl = url.get(i).substring(0,url.get(i).indexOf("?"));
                    attribute1.put("picture",imurl);
                    System.out.print("imurl为：");
                    System.out.println(imurl);
                    repairTaskMapper.insertRepairTaskPicture(attribute1);
                }
                return "上传成功";
            }
        }



    }

    @Override
    public List<String> getPicUrl(List<MultipartFile> files) {
        List<String> url = ossUtil.checkList(files);
        System.out.print("url为：");
        System.out.println(url);
        String[] imgurl = new String[3];
        for(int i = 0;i<url.size() ;i++){
            String imurl = url.get(i).substring(0,url.get(i).indexOf("?"));
            imgurl[i] = imurl;
            System.out.print("imurl为：");
            System.out.println(imurl);
        }
        System.out.println(imgurl);
        return null;
    }

    @Override
    public void modifyRepairStatus(int id, String status) {
        if(status.equals("处理中")||status.equals("未处理")){
            String endDate = null;  //这两种情况不需要填写结束日期
            repairTaskMapper.modifyTaskStatus(id,status,endDate);

        }else{   //指取消以及已完成
            String endDate = new Date(new java.util.Date().getTime()).toString();
            repairTaskMapper.modifyTaskStatus(id,status,endDate);
        }
    }


    @Transactional(rollbackFor=Exception.class,readOnly = true)
    @Override
    public List<String> userQuerySelfTask(String phoneNumber) {

        return repairTaskMapper.queryTaskByPhoneNumber(phoneNumber);
    }
    @Transactional(rollbackFor=Exception.class,readOnly = true)
    @Override
    public int queryUserTaskNumsByPhoneNumber(String phoneNumber) {
        return repairTaskMapper.queryTaskNumsByPhoneNumber(phoneNumber);
    }
    @Transactional(rollbackFor=Exception.class)
    @Override
    public void canceluserTask(int id,String status,String phoneNumber,String limitstatus) {
        Map<String, Object> attribute = new HashMap<>();
        attribute.put("id",id);
        attribute.put("phoneNumber",phoneNumber);
        attribute.put("status",status);
        attribute.put("limitstatus",limitstatus);
        repairTaskMapper.cancelUserTask(attribute);
    }

    @Transactional(rollbackFor=Exception.class,readOnly = true)
    @Override
    public List usergetTaskDetail(int id, String phoneNumber) {
        Map<String, Object> attribute = new HashMap<>();
        attribute.put("id",id);
        attribute.put("telephone",phoneNumber);

        return repairTaskMapper.usergetTaskDetail(attribute);
    }

    @Override
    public List<Task> memberQuerySelfTask(String account) {
        return repairTaskMapper.queryTaskByMemberAccount(account);
    }

    @Override
    public List<Task> getTaskDetailById(int id) {
        List<Task> task = repairTaskMapper.getTaskDetailById(id);
        List<String> url = repairTaskMapper.getTaskPicture(id);
        task.get(0).setPictureUrl(url);
        return task;
    }

    @Override
    public String getTaskLog(int id){
        return repairTaskMapper.getTaskLog(id);
    }

    @Override
    public List<Task> memberQueryAllTask(int page, int limit) {
        int start = (page - 1) * limit;
        return repairTaskMapper.queryAllTask(start,limit);
    }

    @Override
    public List<Task> memberQueryAllOfflineTask(int page, int limit, String method) {
        int start = (page - 1) * limit;
        return repairTaskMapper.queryAllOfflineTask(start,limit,method);
    }

    @Override
    public List<Task> memberQueryOfflineTaskByStatus(int page, int limit, String method, String status) {
        int start = (page - 1) * limit;
        return repairTaskMapper.queryOfflineTaskByStatus(start,limit,method,status);
    }

    @Override
    public List<Task> memberQueryAllOnlineTask(int page, int limit, String method) {
        int start = (page - 1) * limit;
        return repairTaskMapper.queryAllOfflineTask(start,limit,method);
    }

    @Override
    public List<Task> memberQueryOnlineTaskByStatus(int page, int limit, String method, String status) {
        int start = (page - 1) * limit;
        return repairTaskMapper.queryOnlineTaskByStatus(start,limit,method,status);
    }

    @Transactional(rollbackFor=Exception.class)
    @Override
    public void setParticipant(int id, List<String> idList, List<String> nameList) {
        repairTaskMapper.deleteTaskMemberByid(id);
        Map<String,Object> attribute = new HashMap<>();
        attribute.put("id",null);//主键设置了自动递增 插入时写一下
        attribute.put("taskid",id);
        for(int i=0;i<idList.size();i++){
            //这里原来是要判断id存不存在的 但是由于传过来的数据就是由数据库里成员列表返回渲染的 相当于检验
            attribute.put("account",idList.get(i));
            attribute.put("name",nameList.get(i));
            repairTaskMapper.setParticipant(attribute);
        }
    }


}
